﻿Imports ComponentPro.Net.Mail
Imports ComponentPro.Net
Imports System.Configuration
Imports ComponentPro.Pdf
Imports System.IO

Namespace LightSwitchApplication
    Public Class EmailAdapter
        Private ReadOnly Property Application As Global.LightSwitchApplication.Application
            Get
                Return Global.LightSwitchApplication.Application.Current
            End Get
        End Property

        Public Sub SendEmail(toEmailAddresses As IEnumerable(Of String), fromEmailAddress As String, subject As String, message As String, pdfDoc() As Byte, IncludeMedia As Boolean, Optional ByVal ccEmailAddresses As IEnumerable(Of String) = Nothing, Optional ByVal bccEmailAddresses As IEnumerable(Of String) = Nothing, Optional ByVal isHtml As Boolean = False)
            Dim userName = ConfigurationManager.AppSettings("SendGridUserId").ToString()
            Dim password = ConfigurationManager.AppSettings("SendGridPassword").ToString()
            Dim smtpServer = ConfigurationManager.AppSettings("SendGridServer").ToString()
            Dim smtpInstance As Smtp = New Smtp
            Dim msg As New MailMessage()
            Dim pdfattach As New Attachment(pdfDoc, "PDFDoc.pdf")
            Try

                ComponentPro.Security.SecuritySettings.MediumTrust = True


                ' Login to the server. 
                smtpInstance.Connect(smtpServer, 465, SecurityMode.Implicit)
                smtpInstance.Authenticate(userName, password)


                ' Create a new mail message. 

                'msg.Subject = "Test Subject"
                'msg.BodyText = "Name"
                msg.From.Add(fromEmailAddress)
                'msg.From = "onlyouknowho@gmail.com"
                'msg.To = "frank.h.meyers@gmail.com"

                ' Send the message. 


                ' for each to email address, add the address to the To collection
                ' of the new message.
                For Each recipient As String In toEmailAddresses
                    msg.To.Add(recipient)
                Next

                If ccEmailAddresses IsNot Nothing Then
                    For Each ccRecipient As String In ccEmailAddresses
                        msg.Cc.Add(ccRecipient)
                    Next
                End If

                ' for each blind carbon copy email address, add the address to the 
                ' Bcc collection propery of the new message.
                If bccEmailAddresses IsNot Nothing Then
                    For Each bccRecipient As String In bccEmailAddresses
                        msg.Bcc.Add(bccRecipient)
                    Next
                End If

                ' set the sender of the message by creating a new 
                ' System.Net.Mail.MailMessage object using the from 
                ' email address.


                ' check whether or not the email messsage should be sent in HTML format.
                'If isHtml Then
                'msg.Load(message)
                'Else
                msg.BodyText = message
                'End If

                If IncludeMedia Then
                    'Dim pdfattach As New Attachment(pdfDoc, "PDFDoc.pdf")
                    msg.Attachments.Add(pdfattach)
                Else

                End If

                ' add the subject to the email message.
                msg.Subject = subject
                'Dim pdfdoc2 As New PdfDocument(PdfDocumentConformanceLevel.Pdf_A1B)
                'pdfdoc2 = pdfDoc
                'Dim pdfstream As Stream = New MemoryStream()
                'pdfdoc2.Save(pdfstream)
                'MsgBox(pdfDoc.Length.ToString)
                ' send the email using the SendGridMail api.
                smtpInstance.Send(msg)

                ' ... 

                'Console.WriteLine("Message sent successfully.")

                ' Disconnect. 
                'smtpInstance.Disconnect()
            Catch ex As Exception
                'Dim xerr = Me.DataWorkspace.ApplicationData.ErrorsSet.AddNew
                'xerr.ErrorDescription = ex.Message
            Finally
                userName = Nothing
                password = Nothing
                smtpServer = Nothing
                pdfattach = Nothing
                msg = Nothing
                smtpInstance.Disconnect()
                smtpInstance.Dispose()
            End Try

        End Sub



    End Class
End Namespace

